<?php
declare(strict_types=1);

namespace App\Master\Framework\Library\Tencent;

use App\Master\Framework\Library\Library;
use App\Utils\Common;

class FaceCheck extends Library {
    protected string $base_uri = 'https://kyc1.qcloud.com';
    protected string $appId = '';
    protected string $appSecret = '';

    public function getAccessToken()
    {
        $data = [
            'appId' => $this->appId,
            'secret' => $this->appSecret,
            'grant_type' => 'client_credential',
            'version' => '1.0.0',
        ];
        $response = $this->getJson("/api/oauth2/access_token", $data);
        if ($response->getStatusCode() != 200) {
            return $this->error($response->getReasonPhrase());
        }

        $json = $response->getBody()->getContents();
        $body = json_decode($json, true);
        if (!isset($body['code']) || $body['code'] != '0') {
            return $this->error(!empty($body['msg']) ? $body['msg'] : 'Error_0', $body);
        }

        return $this->success('获取成功', $body);
    }

    /**
     * 获取 SIGN ticket
     * @return bool
     */
    public function getTicket()
    {
        if (!$this->getAccessToken()) {
            return $this->error($this->getMessage(),$this->getData());
        }
        $access_token = $this->getData()['access_token'] ?? '';
        $data = [
            'appId' => $this->appId,
            'access_token' => $access_token,
            'type' => 'SIGN',
            'version' => '1.0.0',
        ];
        $response = $this->getJson("/api/oauth2/api_ticket", $data);
        if ($response->getStatusCode() != 200) {
            return $this->error($response->getReasonPhrase());
        }

        $json = $response->getBody()->getContents();
        $body = json_decode($json, true);
        if (!isset($body['code']) || $body['code'] != '0') {
            return $this->error(!empty($body['msg']) ? $body['msg'] : 'Error_0', $body);
        }

        return $this->success('获取成功', $body);
    }

    /**
     * 人脸核身结果查询接口(升级)
     * @param string $orderNo 订单号，字母/数字组成的字符串,是您需要查询结果的人脸核身订单号
     * @param int $getFile 是否需要获取人脸识别的视频和文件，值为1则返回视频和照片、值为2则返回照片、值为3则返回视频；其他则不返回
     * @param string $queryVersion 查询接口版本号(传1.0则返回 sdk 版本号和 trtc 标识)
     * @return bool
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @Notice https://cloud.tencent.com/document/product/1007/61302
     */
    public function getFaceRecord(string $orderNo,int $getFile = 0,string $queryVersion = '')
    {
        if (!$this->getTicket()) {
            return $this->error($this->getMessage(),$this->getData());
        }
        $ticket = $this->getData()['tickets'][0]['value'] ?? '';
        $nonce = Common::createNonceStr(32);

        //获取签名
        $sign_data = [
            'appId' => $this->appId,
            'orderNo'  => $orderNo,
            'version' => '1.0.0',
            'ticket'  => $ticket,
            'nonce'   => $nonce
        ];
        asort($sign_data);//排序
        $sign_string = join('', $sign_data);
        $sign = sha1($sign_string);
        $data = [
            'appId' => $this->appId,
            'version' => '1.0.0',
            'nonce' => $nonce,
            'orderNo' => $orderNo,
            'sign' => $sign,
            'getFile' => $getFile,
            'queryVersion' => $queryVersion
        ];

        $response = $this->postJson("/api/v2/base/queryfacerecord?orderNo={$orderNo}",$data);
        if ($response->getStatusCode() != 200) {
            return $this->error($response->getReasonPhrase());
        }

        $json = $response->getBody()->getContents();
        $body = json_decode($json, true);
        if (empty($body['code']) || $body['code'] != '0') {
            return $this->error(!empty($body['msg']) ? $body['msg'] : 'Error_0', $body ?? []);
        }

        return $this->success('获取成功', $body);
    }
}